/*
 * JDiskHogPanel.java
 *
 * Created on August 20, 2008, 8:21 AM
 */

package jdiskhog;

import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import javax.swing.AbstractAction;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.tree.TreePath;

/**
 *
 * @author  jbf
 */
public class JDiskHogPanel extends javax.swing.JPanel {

    /** Creates new form JDiskHogPanel */
    public JDiskHogPanel() {
        initComponents();
        jTree1.addMouseListener( createMouseListener(jTree1) );
        
    }

    private MouseListener createMouseListener(final JTree jtree) {
        return new MouseAdapter() {

            TreePath context;

            @Override
            public void mousePressed(MouseEvent e) {
                
                if (e.isPopupTrigger()) {
                    context = jtree.getPathForLocation(e.getX(), e.getY());
                    jtree.getSelectionModel().addSelectionPath(context);
                    if (context != null) {
                        showPopup(e);
                    }
                }
            }
            JPopupMenu popup;

            private void showPopup(MouseEvent e) {
                if (popup == null) {
                    popup = new JPopupMenu();
                    popup.add( new JMenuItem( new AbstractAction("Delete") {

                        public void actionPerformed(ActionEvent e) {
                            FSTreeModel model = (FSTreeModel) jtree.getModel();
                            
                            TreePath[] paths= jtree.getSelectionPaths();
                            
                            boolean okay= true;
                            IllegalArgumentException ex=null;
                            
                            for ( int i=0; i<paths.length; i++ ) {
                                File f = model.getFile(paths[i]);
                                
                                if ( f.equals(model.root) ) continue;
                                if (f.isFile()) {
                                    okay = f.delete();
                                } else {
                                    try {
                                        okay = Util.deleteFileTree(f);
                                    } catch ( IllegalArgumentException ex1 ) {
                                        ex= ex1;
                                        okay= false;
                                    }
                                }
                            }
                            
                            if (!okay) {
                                JOptionPane.showConfirmDialog(jtree, ex.getLocalizedMessage(),"unable to delete", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE );
                            }

                            scan(model.root);

                        }
                    }));
                }
                popup.show(jtree, e.getX(), e.getY());
            }
        };
    }

    public void scan( File root ) {
        DiskUsageModel dumodel = new DiskUsageModel();
        dumodel.search(root, 0, this.pMComponent1 );
        FSTreeModel model = new FSTreeModel(dumodel, root);
        jTree1.setModel(model);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        pMComponent1 = new jdiskhog.PMComponent();

        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTree1.setName("jTree1"); // NOI18N
        jScrollPane1.setViewportView(jTree1);

        pMComponent1.setName("pMComponent1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pMComponent1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pMComponent1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTree jTree1;
    private jdiskhog.PMComponent pMComponent1;
    // End of variables declaration//GEN-END:variables

}
